<legend><strong>广告规则</strong>: <%=@advertise_group.name%></legend>
<%= form_for :rules, :as => :post, :url => update_rules_admin_recruitment_group_path(@advertise_group),:class => "simple_form form-horizontal", remote: true do |f| %>
<div class="tree smart-form">
  <ul>
		<li>
			<span><i class="fa fa-lg fa-folder-open"></i> 中国</span>
			<ul>
	  	<% Province.all.each do |province| %>
	  		<li id="area-province-<%= province.id %>">
	    		<span>
	    			<i class="fa fa-lg <%= province.cities.present? ? 'fa-plus-circle' : 'fa-minus-circle' %>"></i>
	    			<%= province.name %> 
	    		</span>
	    		<%= rule_check_box f, province, nil, @advertise_group %>
	    		<ul>
	  			<% province.cities.each do |city| %>
	  				<li id="area-city-<%= city.id %>">
	  					<span>
	  					<i class="fa fa-lg <%= city.terminal_groups.present? ? 'fa-plus-circle' : 'fa-minus-circle' %>"></i> 
	  					<%= city.name %>
	  					</span>
	  					<%= rule_check_box f, city, province, @advertise_group %>
	  					<ul>
	  						<% city.terminal_groups.each do |tg| %>
	  							<li>
	  							<span>
	  								<i class="icon-leaf"></i> <%= tg.group_name %>
	  							</span>
	  							<%= rule_check_box f, tg, city, @advertise_group %>
	  							</li>
	  						<% end %>
	  					</ul>
	  				</li>
	  			<% end %>
	    		</ul>
	  		</li>
	  	<% end %>
	  	</ul>
	  </li>
  </ul>
</div>
<%= f.submit "Update",confirm: "Are you sure?",class: "btn btn-primary" %>
<% end %>

<script type="text/javascript">
	$(document).ready(function(){

	  pageSetUp();
	  // PAGE RELATED SCRIPTS

	  $('.tree > ul').attr('role', 'tree').find('ul').attr('role', 'group');
	  $('.tree').find('li:has(ul)').addClass('parent_li').attr('role', 'treeitem').find(' > span').attr('title', 'Collapse this branch').on('click', function(e) {
	    var children = $(this).parent('li.parent_li').find(' > ul > li');
	    if (children.is(':visible')) {
	      children.hide('fast');
	      $(this).attr('title', 'Expand this branch').find(' > i').removeClass().addClass('fa fa-lg fa-plus-circle');
	    } else {
	      children.show('fast');
	      $(this).attr('title', 'Collapse this branch').find(' > i').removeClass().addClass('fa fa-lg fa-minus-circle');
	    }
	    e.stopPropagation();
	  });

    $(".cities").click(function() {
    	if(this.checked){
    		$("." + this.id.replace("rules_","")).each(function(index,tg){

    			tg.checked = true
    		})
    	} else {
    		$("." + this.id.replace("rules_","")).each(function(index,tg){
    			tg.checked = false
    		})    		
    	}  
    });

    $(".provinces").click(function() {
    	if(this.checked){
    		$("." + this.id.replace("rules_","")).each(function(index,city){
    			if(city.checked == false)
  					$(city).trigger("click")
    		})
    	} else {
    		$("." + this.id.replace("rules_","")).each(function(index,city){
    			if(city.checked == true)
  					$(city).trigger("click")
    		})    		
    	}  
    });
	});
</script>